package com.rzdata.poct.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rzdata.poct.common.core.mybatisplus.core.ServicePlusImpl;
import com.rzdata.poct.common.core.page.PagePlus;
import com.rzdata.poct.common.core.page.TableDataInfo;
import com.rzdata.poct.common.utils.PageUtils;
import com.rzdata.poct.common.utils.StringUtils;
import com.rzdata.poct.system.domain.business.bo.PoctTestRecordsBo;
import com.rzdata.poct.system.domain.business.po.PoctTestRecords;
import com.rzdata.poct.system.domain.business.vo.PoctTestRecordsVo;
import com.rzdata.poct.system.mapper.PoctTestRecordsMapper;
import com.rzdata.poct.system.service.IPoctTestRecordsService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2021-09-09
 */
@Service
public class PoctTestRecordsServiceImpl extends ServicePlusImpl<PoctTestRecordsMapper, PoctTestRecords, PoctTestRecordsVo> implements IPoctTestRecordsService {

    @Override
    public PoctTestRecordsVo queryById(Long id){
        return getVoById(id);
    }

    @Override
    public TableDataInfo<PoctTestRecordsVo> queryPageList(PoctTestRecordsBo bo) {
        PagePlus<PoctTestRecords, PoctTestRecordsVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo));
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public IPage<PoctTestRecordsVo> listPage(PoctTestRecordsBo bo) {
        Page<PoctTestRecordsVo> page = new Page<>(bo.getPageNum(), bo.getPageSize());
        return this.baseMapper.listPage(page, bo);
    }

    @Override
    public List<PoctTestRecordsVo> queryList(PoctTestRecordsBo bo) {
        return listVo(buildQueryWrapper(bo));
    }

    private LambdaQueryWrapper<PoctTestRecords> buildQueryWrapper(PoctTestRecordsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PoctTestRecords> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getUserName()), PoctTestRecords::getUserName, bo.getUserName());
        lqw.eq(bo.getDeptId() != null, PoctTestRecords::getDeptId, bo.getDeptId());
        lqw.eq(StringUtils.isNotBlank(bo.getSubjects()), PoctTestRecords::getSubjects, bo.getSubjects());
        lqw.like(StringUtils.isNotBlank(bo.getTestName()), PoctTestRecords::getTestName, bo.getTestName());
        lqw.eq(bo.getScore() != null, PoctTestRecords::getScore, bo.getScore());
        lqw.eq(bo.getTotalScore() != null, PoctTestRecords::getTotalScore, bo.getTotalScore());
        lqw.eq(bo.getQualifiedScore() != null, PoctTestRecords::getQualifiedScore, bo.getQualifiedScore());
        lqw.eq(bo.getTestTime() != null, PoctTestRecords::getTestTime, bo.getTestTime());
        lqw.eq(StringUtils.isNotBlank(bo.getTestResult()), PoctTestRecords::getTestResult, bo.getTestResult());
        return lqw;
    }

    @Override
    public Boolean insertByBo(PoctTestRecordsBo bo) {
        PoctTestRecords add = BeanUtil.toBean(bo, PoctTestRecords.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByBo(PoctTestRecordsBo bo) {
        PoctTestRecords update = BeanUtil.toBean(bo, PoctTestRecords.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(PoctTestRecords entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
